<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * @param mprogram $mprogram
 * 
 *
 */
class uindex extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'frontend/layout',
        'left' => 'frontend/include/left',
        'right' => 'frontend/include/right',
    );

    function __construct() {
        parent::__construct();
        $this->load->model("mvideo");
        $this->load->model("mprogram");
        $this->load->model("mseries");
        $this->load->model("mcategory");
    }

    public function index($page = 0) {
        $data = array();
        $this->db->order_by('PROG_ID desc');
        $this->db->limit(10);
        $json = $this->config->item('PROGRAM_SLIDER');
		if (json_decode($json)) {
			$data['programs'] = $this->mprogram->where_in(json_decode($json), "PROG_ID");
		}else {
			$data['programs'] = $this->mprogram->gets(array(
            'PROG_STATUS' => 1,
			));
		}
        
        $json_home= $this->config->item('PROGRAM_HOMEPAGE');
		
		$data['series']  = $this->mseries->gets_in_with_program (json_decode($json_home));
		
        $data['seo'] = $this->seo->get('trang-chu', $page);
		
		$this->theme_data["layout_main"] = "frontend/layout_program";
        $this->theme_data["main"] = "frontend/uindex/home";
        $this->template->view($this->theme_data, $data);
    }

    function detail($videoCode = '') {
        $data = array();
        if ($videoCode == '')
            redirect(page404(), 301);
        $videoID = explode("-", $videoCode);
        $videoID = end($videoID);
        
        $data['programCode'] = 	$this->uri->segment(1);
        
        if (!$videoID || $videoID <= 0)
            redirect(page404(), 301);

        $data['video'] = $this->mvideo->get($videoID, 'VID_ID');
        $data['program'] = $this->mprogram->get($data['programCode'], 'PROG_CODE');
		
		$this->db->where('SER_ID !=', $data['video'] -> SER_ID);
		$data['series'] = $this->mseries->gets(array(
			'PROG_ID' => $data['program']->PROG_ID
			));
		
		$data['videoID'] = $videoID;
        if (!$data['video'])
            redirect(page404(), 301);
        
        $this->mvideo->updateView($videoID);
		
        $data['series_videos'] = $this->mvideo->gets(array(
        	'SER_ID' => $data['video'] -> SER_ID
        )); 
        
        $data["seo"] = $this->seo->set($data["video"]);
        $this->theme_data["main"] = "frontend/uindex/detail";
        $this->theme_data["layout_main"] = "frontend/layout_video";
        $this->template->view($this->theme_data, $data);
    }

    function sample($page = 0) {
        $page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 20;
        $config['cur_page'] = $page;
        $config['base_url'] = $this->template->url_page;
        $config['total_rows'] = $this->mvideo->num(1, 'VID_STATUS');
        $this->pagination->initialize($config);
        $config['first_url'] = $config['base_url'] . ".html";
    }

    function category($categoryCode, $page = 0) {
		$data = array();
		
		$data['category'] = $this->mcategory->get(array(
            'CAT_CODE' => $categoryCode,
        ));
		if (!$data['category'])
			redirect(page404(), 301);
		
		$data["seo"] = $this->seo->set($data["category"]);
		$data['subcategory'] = $this->mcategory->get(array(
            'CAT_CODE' => $categoryCode,
        ));
		$this->db->limit(10);
		$data['programs'] = $this->mprogram->gets($data["category"]->CAT_ID, 'CAT_ID');
		
		$this->theme_data["main"] = "frontend/uindex/category";
        $this->theme_data["layout_main"] = "frontend/layout_program";
        $this->template->view($this->theme_data, $data);
    }

    function program($programCode, $page = 0) {
        $data = array();
		
        $data['program'] = $this->mprogram->get(array(
            'PROG_CODE' => $programCode,
        ));
        $data['series'] = $this->mseries->gets_by_program($data['program']->PROG_ID);
        $data["seo"] = $this->seo->set($data['program'], $page);

        $this->theme_data["main"] = "frontend/uindex/program";
        $this->theme_data["layout_main"] = "frontend/layout_program";
        $this->template->view($this->theme_data, $data);
    }

    function series($seriesCode, $page = 0) {
        $series = $this->mseries->get(array(
            'SER_CODE' => $seriesCode,
        ));
        $program = $this->mprogram->get(array(
        	'PROG_ID' => $series->PROG_ID
        ));
        $video = $this->mvideo->get(array(
            'SER_ID' => $series->SER_ID,
        ));
        redirect (video_url($video->VID_ID,$video->VID_CODE, $program->PROG_CODE ));
    }
	
}
